package com.amado.course50java8;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Java812Comparetor {

    public static void main(String[] args) {
        List<Student> students1 = Arrays.asList(new Student(18, 20), new Student(20, 20), new Student(20, 22));
        List<Student> students2 = Arrays.asList(new Student(18, 20), new Student(20, 20), new Student(20, 22));
        //默认是升序，e1-e2,需要降序使用e2-e1,大于0时则换位置
        students1.sort((e1, e2) -> {
            int sort = e2.getScore() - e1.getScore();
            if (sort == 0) {
                sort = e2.getAge() - e1.getAge();
            }
            return sort;
        });
        students1.forEach(System.out::println);
        System.out.println("--------------");
        Comparator<Student> c1 = (e1,e2)->e2.getScore()-e1.getScore();
        Comparator<Student> c2 = (e1,e2)->e2.getAge()-e1.getAge();
        students2.sort(c1.thenComparing(c2));
        students2.forEach(System.out::println);
    }

    static class Student {
        private int age;
        private int score;

        public Student(int age, int score) {
            this.age = age;
            this.score = score;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "age=" + age +
                    ", score=" + score +
                    '}';
        }
    }

}
